package classmate.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;

import classmate.po.Room;
import classmate.po.Student;
import classmate.service.ClassmateService;
import classmate.view.StudentDialog;
import classmate.view.StudentFrame;

public class ClassmateControllerListener implements ActionListener {
	private StudentFrame classmateFrame;
	private ClassmateService classmateService;
	private StudentDialog studentDialog;

	public ClassmateControllerListener() {
		super();
		classmateService = new ClassmateService();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// 打开新增学生对话框
		if ("addStudentDialog".equals(e.getActionCommand())) {
			studentDialog = new StudentDialog(classmateFrame, "添加学生信息", null, null);
		}
		// 新增提交
		else if ("addStudent".equals(e.getActionCommand())) {
			Student newStudent = this.getStudentInstance();
			if (newStudent != null) {
				classmateService.addStudent(newStudent);
				JOptionPane.showMessageDialog(classmateFrame, "添加" + newStudent.getName() + "学生成功！");
				studentDialog.dispose();
				classmateFrame.refreshStudent(classmateService.findAllStudent());
			} else {
				JOptionPane.showMessageDialog(classmateFrame, "添加学生失败");
			}

		}

		// 打开查询学生对话框
		else if ("searchStudentDialog".equals(e.getActionCommand())) {
			List<Student> studentList = new ArrayList<Student>();
			for (Student student : classmateService.findAllStudent()) {
				studentList.add(student);
			}
			String str = JOptionPane.showInputDialog(classmateFrame, "输入学生学号");
			if (str != null && !"".equals(str)) {
				if (str.matches("^[0-9]*$")) {
					long id = Long.parseLong(str);
					Student student = new Student();
					student.setId(id);
					List<Student> result = classmateService.getStudent(id);
					classmateFrame.showStudentSearchResult(result);
				} else {
					JOptionPane.showMessageDialog(classmateFrame, "输入的不是数字", "输入错误", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
		// 打开修改学生对话框
		else if ("updateStudentDialog".equals(e.getActionCommand())) {
			int selRow = classmateFrame.getSelRow();
			if (selRow < 0) {
				JOptionPane.showMessageDialog(classmateFrame, "请选择一行数据！");
			} else {
				Student student = classmateService.findAllStudent().get(classmateFrame.getSelRow());
				Room room = classmateService.findAllRoom().get(classmateFrame.getSelRow());
				studentDialog = new StudentDialog(classmateFrame, "修改学生信息", student, room);
			}
		}
		// 新增修改
		else if ("updateStudent".equals(e.getActionCommand())) {
			Student updateStudent = this.getStudentInstance();
			if (updateStudent != null) {
				classmateService.updateStudent(classmateFrame.getSelRow(),updateStudent);
				JOptionPane.showMessageDialog(classmateFrame, updateStudent);
				studentDialog.dispose();
				classmateFrame.refreshStudent(classmateService.findAllStudent());
			} else {
				JOptionPane.showMessageDialog(classmateFrame, "更新学生信息失败");
			}

		} else if ("deleteStudent".equals(e.getActionCommand())) {
			int selRow = classmateFrame.getSelRow();
			if (selRow < 0) {
				JOptionPane.showMessageDialog(classmateFrame, "请选择一行数据");

			} else {
				Student student = classmateService.findAllStudent().get(classmateFrame.getSelRow());
				classmateService.deleteStudent(student.getId());
				JOptionPane.showMessageDialog(classmateFrame, "删除学号为" + student.getId() + "的学生信息成功");
				classmateFrame.refreshStudent(classmateService.findAllStudent());
			}
		} else if ("refreshStudentFrame".equals(e.getActionCommand())) {
			classmateFrame.refreshStudent(classmateService.findAllStudent());
		}
	}

	private Student getStudentInstance() {
		long id = 0;
		String name = null;
		Date birthday = null;
		String phoneNumber = null;
		String email = null;
		String nativePlace = null;
		String address = null;
		int roomNo = 0;
		Student student = null;

		String idInput = studentDialog.getIdTextField().getText().trim();
		if (idInput != null && !"".equals(idInput)) {
			if (idInput.matches("^[0-9]*$")) {
				id = Long.parseLong(idInput);
			} else {
				JOptionPane.showMessageDialog(classmateFrame, "学号必须是整数类型", "输入错误", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			JOptionPane.showMessageDialog(classmateFrame, "学号不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		String nameInput = studentDialog.getNameTextField().getText().trim();
		if (nameInput != null && !"".equals(nameInput)) {
			name = nameInput;
		} else {
			JOptionPane.showMessageDialog(classmateFrame, "名称不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		String birthdayInput = studentDialog.getBirthdayTextField().getText().trim();
		if (birthdayInput != null && !"".equals(birthdayInput)) {
			if (birthdayInput.matches("\\d{4}-\\d{2}-\\d{2}")) {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				try {
					birthday = sdf.parse(birthdayInput);
				} catch (ParseException e) {
					e.printStackTrace();
				}
			} else {
				JOptionPane.showMessageDialog(classmateFrame, "生日必须是日期类型", "输入错误", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			JOptionPane.showMessageDialog(classmateFrame, "生日不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		String phoneNumberInput = studentDialog.getPhoneNumberTextField().getText().trim();
		if (phoneNumberInput != null && !"".equals(phoneNumberInput)) {
			if (phoneNumberInput.matches("^[0-9]*$")) {
				phoneNumber = phoneNumberInput;
			} else {
				JOptionPane.showMessageDialog(classmateFrame, "手机号码必须是数字类型", "输入错误", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			JOptionPane.showMessageDialog(classmateFrame, "手机号码不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		String emailInput = studentDialog.getEmailTextField().getText().trim();
		if (emailInput != null && !"".equals(emailInput)) {
			if (emailInput.matches("^[a-z0-9A-Z]+[- | a-z0-9A-Z . _]+@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-z]{2,}$")) {
				email = emailInput;
			} else {
				JOptionPane.showMessageDialog(classmateFrame, "电子邮箱必须是电子邮箱类型", "输入错误", JOptionPane.ERROR_MESSAGE);
			}
		} else {
			JOptionPane.showMessageDialog(classmateFrame, "电子邮箱不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		String nativePlaceInput = studentDialog.getNativePlaceTextField().getText().trim();
		if (nativePlaceInput != null && !"".equals(nativePlaceInput)) {
			nativePlace = nativePlaceInput;
		} else {
			JOptionPane.showMessageDialog(classmateFrame, "籍贯不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		String addressInput = studentDialog.getAddressTextField().getText().trim();
		if (addressInput != null && !"".equals(addressInput)) {
			address = addressInput;
		} else {
			JOptionPane.showMessageDialog(classmateFrame, "家庭地址不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		
		String roomNoInput = studentDialog.getRoomIdTextField().getText().trim();
		if (roomNoInput != null && !"".equals(roomNoInput)) {
			roomNo = Integer.parseInt(roomNoInput);
		} else {
			JOptionPane.showMessageDialog(classmateFrame, "宿舍不能为空", "输入错误", JOptionPane.ERROR_MESSAGE);
		}
		if (id > 0 && name != null && birthday != null && phoneNumber != null && email != null && nativePlace != null
				&& address != null && roomNo >0) {
			student = new Student(id, name, birthday, phoneNumber, email, nativePlace, address, roomNo);
		}
		return student;
	}
	
	

	public void setClassmateFrame(StudentFrame classmateFrame) {
		this.classmateFrame = classmateFrame;
	}

	public void setStudentDialog(StudentDialog studentDialog) {
		this.studentDialog = studentDialog;
	}

	public ClassmateService getClassmateService() {
		return classmateService;
	}

}
